package gd.qaq.blue.bluesname.prompt;

import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.ValidatingPrompt;
import org.bukkit.entity.Player;

import java.util.List;

import static gd.qaq.blue.bluesname.BluesName.colorFormat;
import static gd.qaq.blue.bluesname.BluesName.config;

/**
 * 反编译的同学~好好学着点噢
 * 作者:blue qq:862899738
 */
@SuppressWarnings("all")
public class ChangeNamePrompt extends ValidatingPrompt {
    private Boolean isPrefix = false;
    private Player player;

    public ChangeNamePrompt(Boolean isPrefix, Player player) {
        this.isPrefix = isPrefix;
        this.player = player;
    }

    public String getPromptText(final ConversationContext context) {
        return this.isPrefix ? colorFormat("&a请在聊天框输入你的&b前缀&a称号") : colorFormat("&a请在聊天框输入你的&e后缀&a称号");
    }

    protected Prompt acceptValidatedInput(final ConversationContext context, final String in) {
        String container_prefix = config.getString("container_prefix");
        String container_suffix = config.getString("container_suffix");
        String input = in;
        if (StringUtils.isNotBlank(container_prefix)) {
            input = container_prefix + input;
        }
        if (StringUtils.isNotBlank(container_suffix)) {
            input += container_suffix;
        }
        Bukkit.dispatchCommand((CommandSender) Bukkit.getConsoleSender(), "manuaddv " + this.player.getName() + (isPrefix ? " prefix " : " suffix ") + input);
        context.getForWhom().sendRawMessage((colorFormat(isPrefix ? "&a你的&b前缀&a称号已更变为:" : "&a你的&e后缀&a称号已更变为:")) + colorFormat("&f" + input));
        return Prompt.END_OF_CONVERSATION;
    }

    protected boolean isInputValid(final ConversationContext context, final String input) {
        List<String> blackList = config.getStringList("blackList");
        if (input.replaceAll("(&.{1}|§.{1}|\\[|\\]|\\(|\\)|【|】)", "").length() > config.getInt("maxLength")) {
            context.getForWhom().sendRawMessage(colorFormat("&c称号长度超过限制,请重新输入(最大长度:&f" + config.getInt("maxLength") + "&c个字)"));
            return false;
        } else if (blackList != null && blackList.size() > 1) {
            for (String blackString : blackList) {
                if (input.contains(blackString)) {
                    context.getForWhom().sendRawMessage(colorFormat("&c称号中包含禁止的词汇:&f" + blackString));
                    return false;
                }
            }
        }
        return true;
    }
}
